package com.wangyadong.hobby.schedule.jobtask.dsa;

public class HasCycle {


    public boolean hasCycle(ListNode node) {
        if (node == null) {
            return false;
        }
        ListNode fast = node.next;
        ListNode slower = node;
        while (fast != null && fast.next != null) {
            if (fast == slower) {
                return true;
            }
            fast = fast.next.next;
            slower = slower.next;
        }
        return false;
    }


    public static class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
            next = null;
        }
    }

    public static void main(String[] args) {
        ListNode node = new ListNode(1);
        ListNode node1 = new ListNode(2);
        node.next = node1;
        ListNode node2 = new ListNode(3);
        node1.next = node2;
        ListNode node3 = new ListNode(4);
        node2.next = node3;
//        node3.next = node2;
        HasCycle hasCycle = new HasCycle();
        System.out.println(hasCycle.hasCycle(node));
    }
}
